
import { ScoreWriteStorage, ScoreStorage, ScoreTimeStorage } from '/@/utils/storage';
import { defineStore } from 'pinia';
import { useUserApi } from '@/api/user';
import { useSystemApi } from '@/api/system';
import { CryptoUtils, RKUtils } from '@/utils';
import { useUserStore } from '@/stores/modules/store-user';


export const useSettingStore = defineStore('setting', {
	state: () => ({
		// 系统模式：['custom', 'develop', 'development', 'production']
		envMode: import.meta.env.MODE,
		// 加解密密钥
		secretKey: '',
		// license信息
		licenseInfo: '',
		// 是否开启加密
		encryptOpen: false,
		//登录模式  account账号登录  multiple 账号手机号登录 mobile 手机号登录
		loginMode: import.meta.env.VITE_LOGIN_MODE,
		// 是否开启Router的web hash 默认开启
		openWebHash: RKUtils.parseStringToBoolean(import.meta.env.VITE_OPEN_WEB_HASH),
		/**
		 * 个性化功能: ['halfPass']
		 * 	halfPass: 储备项目入库中展示过半数的评审情况列，以及色块提示
		 * 	ndrcStatus: 储备项目入库（按批次）列表中展示发改委审核状态
		 * 	assessStatus: 储备项目入库（按批次）列表中展示评估中心审核状态
		 */
		customModules: RKUtils.parseStringToObject(import.meta.env.VITE_CUSTOM_MODULES),
		// 默认是 true 不隐藏下载  false 隐藏下载(安徽)
		expertDownload: RKUtils.parseStringToBoolean(import.meta.env.VITE_EXPERT_DOWNLOAD),
		// 是否开启主题设置 true-开启 false-关闭
		openThemeSetting: RKUtils.parseStringToBoolean(import.meta.env.VITE_OPEN_THEME_SETTING),
		// 是否开启复制主题按钮 true-开启 false-关闭
		openThemeCopyBtn: RKUtils.parseStringToBoolean(import.meta.env.VITE_OPEN_THEME_COPY_BTN),
		// 是否开启license校验 true-开启 false-关闭
		openLicenseCheck: RKUtils.parseStringToBoolean(import.meta.env.VITE_OPEN_LICENSE_CHECK),
	}),
	actions: {
		/**
		 * 设置密钥
		 * @returns Promise
		 */
		setSecretKey() {
			return new Promise((resolve, reject) => {
				useUserApi()
					.getUserAuth()
					.then((res) => {
						const { code, data, msg } = res;
						if (code === 200) {
							const { signature, transferMode } = data;
							this.secretKey = signature;
							// transferMode: 1-加密 0-不加密
							if (transferMode) {
								this.encryptOpen = (transferMode == '1') ? true : false
							}
							CryptoUtils.setCryptoSecretKey(signature);
							resolve(data);
						} else {
							reject(msg);
						}
					})
					.catch((error) => {
						reject(error);
					});
			});
		},
		/**
		 * 设置 licenseInfo
		 *
		 * @returns Promise
		 */
		setLicenseInfo() {
			return new Promise((resolve, reject) => {
				if (!this.openLicenseCheck) {
					resolve('')
				} else {
					useSystemApi().getLicenseInfo().then(res => {
						const { code, data } = res
						if (200 == code) {
							this.licenseInfo = data
							resolve(data)
						} else {
							resolve("")
						}
					}).catch(err => {
						reject(err)
					})
				}
			})
		},
		/**
		 * 获取评分项缓存数据
		 *
		 * @param {String} key 缓存关键字
		 * @returns 
		 */
		getCacheScore(key) {
			const { userId } = useUserStore().userInfo
			let newKey = `${userId}-${key}`
			let baseKey = btoa(newKey)
			return ScoreStorage.get(baseKey)
		},
		/**
		 * 获取评分项修改标志（评估中心）
		 *
		 * @param {String} key 缓存关键字
		 * @returns 
		 */
		getScoreWrite(key) {
			const { userId } = useUserStore().userInfo
			let newKey = `${userId}-${key}`
			let baseKey = btoa(newKey)
			return ScoreWriteStorage.get(baseKey)
		},
		/**
		 * 获取暂存时间（评估中心）
		 *
		 * @param {String} key 缓存关键字
		 * @returns 
		 */
		getScoreTime(key) {
			const { userId } = useUserStore().userInfo
			let newKey = `${userId}-${key}`
			let baseKey = btoa(newKey)
			return ScoreTimeStorage.get(baseKey)
		},
		/**
		 * 设置评分项缓存数据
		 * 
		 * @param {String} key 缓存关键字
		 * @param {Object} value 缓存的内容
		 */
		setCacheScore(key, value) {
			const { userId } = useUserStore().userInfo
			let newKey = `${userId}-${key}`
			let baseKey = btoa(newKey)
			ScoreStorage.set(baseKey, value, ScoreStorage.expires)
		},
		/**
		 * 设置评分项修改标志（评估中心）
		 * 
		 * @param {String} key 缓存关键字
		 * @param {Object} value 缓存的内容
		 */
		setScoreWrite(key, value) {
			const { userId } = useUserStore().userInfo
			let newKey = `${userId}-${key}`
			let baseKey = btoa(newKey)
			ScoreWriteStorage.set(baseKey, value, ScoreWriteStorage.expires)
		},
		/**
		 * 设置暂存时间（评估中心）
		 * 
		 * @param {String} key 缓存关键字
		 * @param {Object} value 缓存的内容
		 */
		setScoreWriteTime(key, value) {
			const { userId } = useUserStore().userInfo
			let newKey = `${userId}-${key}`
			let baseKey = btoa(newKey)
			ScoreTimeStorage.set(baseKey, value, ScoreTimeStorage.expires)
		},
		/**
		 * 删除指定的评分项缓存
		 *
		 * @param {String} key 缓存关键字
		 */
		removeCacheScore(key) {
			const { userId } = useUserStore().userInfo
			let newKey = `${userId}-${key}`
			let baseKey = btoa(newKey)
			ScoreStorage.remove(baseKey)
		},
		/**
		 * 校验是否存在个性化的模块
		 *
		 * @param {String} key 模块名称
		 * @returns true - 存在，false - 不存在
		 */
		checkCustomModules(key) {
			return this.customModules.includes(key)
		}
	},
});
